package com.aorise.service.order.impl;

import com.aorise.exception.ServiceException;
import com.aorise.mapper.order.WorkCameraMapper;
import com.aorise.mapper.order.WorkOrderMapper;
import com.aorise.model.order.WorkCameraAddVO;
import com.aorise.model.order.WorkOrderAddVO;
import com.aorise.model.order.WorkOrderModel;
import com.aorise.service.order.WorkOrderService;
import com.aorise.utils.ConstDefine;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.page.Page;
import com.aorise.utils.push.AppMessagePush;
import com.aorise.utils.push.PushMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Author:psl
 * @Desicription: 运维工单信息接口实现类
 * @Date:Created in  2019-06-03.
 * @Modified By:
 */
@Service
public class WorkOrderServiceImpl implements WorkOrderService {

    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Autowired
    private WorkCameraMapper workCameraMapper;

    /**
     * 个推appId
     */
    @Value("${getui.appId}")
    private String appId;

    /**
     * 个推appKey
     */
    @Value("${getui.appKey}")
    private String appKey;

    /**
     * 个推masterSecret
     */
    @Value("${getui.masterSecret}")
    private String masterSecret;

    /**
     * 个推host
     */
    @Value("${getui.host}")
    private String host;


    /**
     * @Author:psl
     * @Description 分页查询运维工单信息
     * @params: map 查询条件
     * @params: page 分页
     * @Date:2019-06-03
     * @Return: List<WorkOrderModel> 运维工单信息集合
     * @Modified By:
     */
    @Override
    public List<WorkOrderModel> getAllWorkOrderListByPage(Map<String, Object> map, Page page) {
        List<WorkOrderModel> cameraModels = null;
        Integer loginUserId = getLoginUser().getId();
        map.put("loginUserId", loginUserId);
        /**查询总条数*/
        Long count = workOrderMapper.getAllWorkOrderListTotalByPage(map);
        if (count > 0) {
            page.setTotalCount(count);
            page.setTotalPage(count, page.getEveryPage());
            if (page.getCurrentPage() > 0 && page.getEveryPage() > 0) {
                /**查询分页条数*/
                cameraModels = workOrderMapper.getAllWorkOrderListByPage(map);
            }
        } else {
            page.setTotalCount(0L);
        }
        return cameraModels;
    }

    /**
     * @Author:cat
     * @Description 新增运维工单信息
     * @params: WorkOrderModel 运维工单信息
     * @Date:2019-05-27
     * @Return: Integer 主键
     * @Modified By:
     */
    @Override
    @Transactional
    public Integer addWorkOrder(WorkOrderAddVO workOrderAddVO) {
        /**生成工单编号 年月日+位数顺序增加*/
        String format = getWNumber();
        workOrderAddVO.setWNumber(format);
        workOrderAddVO.setWStatus(ConstDefine.WORK_ORDER_STATUS_WAIT);
        workOrderAddVO.setState(ConstDefine.STATE_ABLE);
        Integer iRet = workOrderMapper.addWorkOrder(workOrderAddVO);
        int id = 0;
        if (iRet > 0) {
            /**新增运维工单设备信息*/
            if (!StringUtils.isBlank(workOrderAddVO.getCameras())) {
                String cameras[] = workOrderAddVO.getCameras().split(",");

                for (int i = 0; i < cameras.length; i++) {
                    WorkCameraAddVO workCameraAddVO = new WorkCameraAddVO();
                    workCameraAddVO.setWorkId(workOrderAddVO.getId());
                    /**设备id*/
                    workCameraAddVO.setCameraId(Integer.parseInt(cameras[i].split(";")[0]));
                    /**异常类型*/
                    workCameraAddVO.setException(Integer.parseInt(cameras[i].split(";")[1]));
                    /**报警时间*/
                    workCameraAddVO.setAlarmTime(cameras[i].split(";")[2]);
                    workCameraMapper.addWorkCamera(workCameraAddVO);
                }
                id = workOrderAddVO.getId();

                PushMessage msgIOS = PushMessage.initial(null, String.valueOf(workOrderAddVO.getMaintUser()),
                        "android", "工单添加", workOrderAddVO.getDescribe(), null, null,
                        null);
                AppMessagePush.sendMessage(msgIOS, appId, appKey, masterSecret, host);
            }else {
                throw new ServiceException(StatusDefineMessage.GetMessage(StatusDefine.ORGAN_EXIST));
            }
        }
        return id;
    }

    /**
     * 获取工单编号
     * @return
     */
    private String getWNumber() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(System.currentTimeMillis());
        WorkOrderModel workOrder =workOrderMapper.selectWorkOrder();
        if(workOrder ==null){
            return format+"1000";
        }
        String wNumber = workOrder.getWnumber();
        Integer substring =null;
        if(StringUtils.isNotBlank(wNumber)){
            substring =Integer.valueOf( wNumber.substring(wNumber.length() - 4, wNumber.length()));
        }else {
            substring =1000;
        }
        wNumber =String.valueOf( format+(++substring));
        return wNumber;
    }
}
